﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VigenereMeter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region Event
        private void menuItemExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void menuItemAbout_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Author\t: Rezan Achmad\nContact\t: rezanachmad@yahoo.co.id\n\nCopyright 2011", "About");
        }

        #region Encrypt
        private void buttonEncrypt_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateEncryptForm())
            {
                editorCipherText.TextBox.Text = Vigenere.Encrypt(editorPlainText.TextBox.Text, textBoxKey.Text);

                if (checkBoxKeyStrengths.IsChecked == true)
                {
                    ShowKeysStrength();
                }
            }
        }
        #endregion

        #region Decrypt
        private void buttonDecrypt_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateDecryptForm())
            {
                editorPlainText.TextBox.Text = Vigenere.Decrypt(editorCipherText.TextBox.Text, textBoxKey.Text);
            }
        }
        #endregion
        #endregion

        #region Key's Strengh
        private void ShowKeysStrength()
        {
            Explanation explanator = new Explanation(editorCipherText.TextBox.Text, textBoxKey.Text);
            explanator.Show();
        }
        #endregion

        #region Validate Form
        private bool ValidateCommonForm()
        {
            // Check Key
            if (textBoxKey.Text == "")
            {
                ShowError("Key must be filled.");
                return false;
            }

            // Check Length of val
            if (textBoxKey.Text.Length > 256)
            {
                ShowError("Max of key's length is 256 character");
                return false;
            }

            return true;
        }

        private bool ValidateDecryptForm()
        {
            if (ValidateCommonForm())
            {
                // Check Cipher Text
                if (editorCipherText.TextBox.Text.Length == 0)
                {
                    ShowError("Cipher text must be filled");
                    return false;
                }
            }

            return true;
        }

        private bool ValidateEncryptForm()
        {

            if (ValidateCommonForm())
            {
                // Check Plain Text
                if (editorPlainText.TextBox.Text.Length == 0)
                {
                    ShowError("Plain text must be filled");
                    return false;
                }
            }

            return true;
        }

        private void ShowError(string p)
        {
            MessageBox.Show(p, "Form Validation");
        }
        #endregion
    }
}
